package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConst;
import com.itheima.constant.RedisConst;
import com.itheima.entity.Result;
import com.itheima.service.MemberService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisPool;

import java.util.Map;

/**
 * 会员登录
 */
@RestController
@RequestMapping("/login")
@Slf4j
public class LoginController {

	@Autowired
	private JedisPool jedisPool;

	@Reference
	private MemberService memberService;

	/**
	 * 1. 获取客户端手机号及验证码
	 * 2. 根据手机号获取redis验证码
	 * 3. 判断用户验证码与redis验证码是否匹配
	 *    1. 如果匹配失败，返回验证码错误
	 *    2.如果成功，根据手机号获取会员信息
	 *        1. 如果有会员，登录成功
	 *        2. 如果不是会员，封装Member对象，调用Service保存
	 * @param map
	 * @return
	 */
	@RequestMapping("/check")
	public Result check(@RequestBody Map<String,String> map){
		log.debug("LoginController: check: " + map);
		// 获取客户端手机号及验证码
		String telephone = map.get("telephone");
		String validateCode = map.get("validateCode");
		// 读取Redis验证码
		String codeInRedis = jedisPool.getResource().get(telephone+ "-"+ RedisConst.SENDTYPE_LOGIN);
		if(codeInRedis == null || codeInRedis.length()==0 ){
			return new Result(false, "验证码失效");
		}
		if ( !codeInRedis.equals(validateCode) ){
			return new Result(false, "验证码错误");
		}
		memberService.saveOrEdit(telephone);
		log.debug("登录成功， 保存会员信息成功！！！");
		return new Result(true, MessageConst.LOGIN_SUCCESS);
	}
}